<?php

class User_helper
{
	function checkUser()
	{
		$CI = get_instance();
	    $CI->load->database();
	    session_start();

		$username = $_COOKIE['lAccount'];
		$password = $_COOKIE['lPassword'];

// 		$username = 'fengshen1';
// 		$password = '654321';

		if ( $username && $password )
		{
			mysql_query("set names latin1");
			$sql = $CI->db->query("SELECT * FROM dflpvmkt.pvSellPoint WHERE Account = '$username'");
			$res = $sql->row_array();
			mysql_query("set names gbk");
			if($res && $res['Password'] == $password){
				$sql = $CI->db->query("select jurisdiction_id from NTSDF.js_jurisdiction_sellpoint where SellPointID = $res[SellPointID] and `application_system_id` = 60");
				$res_admin = $sql->row_array();
				$sql = $CI->db->query("select jurisdiction_id from NTSDF.js_jurisdiction_sellpoint where SellPointID = $res[SellPointID] and `application_system_id` = 59 and jurisdiction_id = 107");
				$res_sell = $sql->row_array();
				if($res_admin['jurisdiction_id']||$res_sell['jurisdiction_id'])
				{
					$_SESSION['baicheng2015_user']=$res;
					$this->getRole($res['SellPointID']);
				}
				else{
					alert('��û��Ȩ�޽���2015�ٳǷ���ϵͳ');
				}
			}else{
				redirect('/');
			}
		}else{
			redirect('/');
		}
	}

	//Ȩ������ 1רӪ��� 2���� 3�ܼ� 4�ܲ� 5�鿴��
	function getRole($sellpointid)
	{
		$CI = get_instance();
		$CI->load->database();
		session_start();

		$_SESSION['baicheng2015_cityLook'] = 0;//�Ƿ��ܲ鿴���л��
		$_SESSION['baicheng2015_cityAdd'] = 0;//�Ƿ���������л��
		$roleInfo = 1;

		//��ѯ�û���ɫ
		$sql = $CI->db->query("select * from NTSDF.js_jurisdiction_sellpoint where SellPointID = ".$_SESSION['baicheng2015_user']['SellPointID']. " and `application_system_id` = 60");
		$res_temp = $sql->row_array();

		if($res_temp['jurisdiction_id'] == 110)
		{
			$roleInfo = 2;
			$_SESSION['baicheng2015_cityLook'] = 1;
		}
		if($res_temp['jurisdiction_id'] == 111)
		{
			$roleInfo = 3;
			$_SESSION['baicheng2015_cityLook'] = 1;
		}
		if($res_temp['jurisdiction_id'] == 112)
		{
			$roleInfo = 4;
			$_SESSION['baicheng2015_cityLook'] = 1;
		}
		if($res_temp['jurisdiction_id'] == 113)
		{
			$roleInfo = 5;
			$_SESSION['baicheng2015_cityLook'] = 1;
		}

		switch ($roleInfo)
		{
			case 1://רӪ���
				$_SESSION['baicheng2015_userRole'] = 1;
				break;
			case 2://������
				$_SESSION['baicheng2015_userRole'] = 2;

				//��ѯ������Ͻ������רӪ��ID
				mysql_query("set names latin1");
				$sql = $CI->db->query("SELECT * FROM dflpvmkt.pvSellPoint WHERE SpState_sys in (1)  and Region_2015 = '".$_SESSION['baicheng2015_user']['Region_2015']."' and SpSArea_2015 = '".$_SESSION['baicheng2015_user']['SpSArea_2015']."' ");
				$res = $sql->result_array();
				mysql_query("set names gbk");

				foreach ($res as $item)
				{
					$baicheng2015_SellPointIDList .= $item['SellPointID'].",";
				}
				$baicheng2015_SellPointIDList .= 0;

				//������Ͻ������רӪ��ID
				$_SESSION['baicheng2015_SellPointIDList'] = $baicheng2015_SellPointIDList;
				break;
			case 3://�ܼ�
				//��ѯ������Ͻ������רӪ��ID
				mysql_query("set names latin1");
				$sql = $CI->db->query("SELECT * FROM dflpvmkt.pvSellPoint WHERE SpState_sys in (1)  and Area_2015 = '".$_SESSION['baicheng2015_user']['Area_2015']."' and Region_2015 = '".$_SESSION['baicheng2015_user']['Region_2015']."' ");
				$res = $sql->result_array();
				mysql_query("set names gbk");

				foreach ($res as $item)
				{
					$baicheng2015_SellPointIDList .= $item['SellPointID'].",";
				}
				$baicheng2015_SellPointIDList .= 0;

				//������Ͻ������רӪ��ID
				$_SESSION['baicheng2015_SellPointIDList'] = $baicheng2015_SellPointIDList;
				$_SESSION['baicheng2015_userRole'] = 3;
				break;
			case 4://�ܲ�
				$_SESSION['baicheng2015_userRole'] = 4;
				break;
			case 5://�鿴
				if(in_array($_SESSION['baicheng2015_user']['Account'],array('lekt','rmp003','caochengkui','wangqian','huwaiqc','maoliying','zhangwq','hakuhodo','rmpallcheck','wenjuan','sunhao','dqtb','pengxb','ewck','xiejianqiang','likedi','zhglk','sunxf','lvzhou'))||$_SESSION['baicheng2015_user']['Area_2015']=='ȫ��')
				{
					$_SESSION['baicheng2015_isLookAll'] = 1;//�Ƿ�鿴ȫ�����
				}
				else
				{
					//��ѯ������Ͻ������רӪ��ID
					mysql_query("set names latin1");
					$sql = $CI->db->query("SELECT * FROM dflpvmkt.pvSellPoint WHERE SpState_sys in (1)  and Area_2015 = '".$_SESSION['baicheng2015_user']['Area_2015']."' ");
					$res = $sql->result_array();
					mysql_query("set names gbk");

					foreach ($res as $item)
					{
						$baicheng2015_SellPointIDList .= $item['SellPointID'].",";
					}
					$baicheng2015_SellPointIDList .= 0;

					//������Ͻ������רӪ��ID
					$_SESSION['baicheng2015_SellPointIDList'] = $baicheng2015_SellPointIDList;
					$_SESSION['baicheng2015_isLookAll'] = 0;
				}
				$_SESSION['baicheng2015_userRole'] = 5;
				break;
		}

		//��ѯ�Ƿ���Ȩ�޲鿴���������Ӫ����
		$sql = $CI->db->query("select * from js_jurisdiction_sellpoint where SellPointID = ".$_SESSION['baicheng2015_user']['SellPointID'] . " and `application_system_id` = 59 and jurisdiction_id = 108");
		$res_temp = $sql->row_array();
		if($res_temp)
		{
			$_SESSION['baicheng2015_cityLook'] = 1;

		}
		$sql = $CI->db->query("select * from js_jurisdiction_sellpoint where SellPointID = ".$_SESSION['baicheng2015_user']['SellPointID'] . " and `application_system_id` = 59 and jurisdiction_id = 109");
		$res_temp = $sql->row_array();
		if($res_temp){
			$_SESSION['baicheng2015_cityLook'] = 1;
			$_SESSION['baicheng2015_cityAdd'] = 1;
		}
		if($roleInfo==1 && $_SESSION['baicheng2015_user']['SpState_sys'] != 1)
		{
			alert("��û��Ȩ�޵�¼!");
			die;
		}

	}

	function getSellPoint($sellpointid)
	{
		$CI = get_instance();
		$CI->load->database();

		mysql_query("set names latin1");
		$sql = $CI->db->query("SELECT * FROM dflpvmkt.pvSellPoint WHERE SellPointID = '$sellpointid'");
		$res = $sql->row_array();
		mysql_query("set names gbk");

		return $res;
	}

	function querySellPoint($area,$Region,$Province,$County,$SellPointName,$keyword)
	{
		$CI = get_instance();
		$CI->load->database();

		if($area)
		{
			$SellPointWhere .= " and Area_2015 = '".$area."'";
		}
		if($Region)
		{
			$SellPointWhere .= " and Region = '".$Region."'";
		}
		if($Province)
		{
			$SellPointWhere .= " and Province like '".$Province."%'";
		}
		if($County)
		{
			$SellPointWhere .= " and County like '".$County."%'";
		}
		if($SellPointName)
		{
			$SellPointWhere .= " and SellPointName = '".$SellPointName."'";
		}
		if($keyword)
			$SellPointWhere .= " and SellPointName like '%$keyword%' ";

		mysql_query("set names latin1");
		$sql = $CI->db->query("SELECT SellPointID FROM dflpvmkt.pvSellPoint WHERE  SpState_sys in (1) ".$SellPointWhere);
		$res_SellPoint = $sql->result_array();
		mysql_query("set names gbk");

		foreach ($res_SellPoint as $item)
		{
			$SellPointIdList .= $item['SellPointID'].",";
		}
		$SellPointIdList .= "0";

		return $SellPointIdList;
	}

}